//
//  AppDelegate.swift
//  SinaWeiBo
//
//  Created by Leon 笑 竹 on 2017/7/31.
//  Copyright © 2017年 Leon 笑 竹. All rights reserved.
//

import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
//        let navi = MainNaviController()
        let tabBar = MainTabBarController()
        
        window =  UIWindow()
        window?.frame = UIScreen.main.bounds
        window?.backgroundColor = UIColor.white
        window?.rootViewController = tabBar
//        navi.addChildViewController(tabBar)
        window?.makeKeyAndVisible()
        
        
        loadData()
        openNoti()
        
        return true
    }
    
    
    //开启通知(开启程序图标的授权)
    func openNoti(){
        
        if #available(iOS 10.0, *){//10.0以上
            
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .carPlay, .sound, .badge], completionHandler: { (success, error) in
                
                print("授权"  + (success ? "成功" : " 失败"))
            })
            
        }else{//10.0以下
            
            let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound],categories: nil)
            UIApplication.shared.registerUserNotificationSettings(settings)
        }
    }
}



extension AppDelegate {
    
    //模拟加载网络数据
    func loadData() {
        
        DispatchQueue.global().async {
            
            //path
            let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            
            //append
            let jsonPath = (path as NSString).appendingPathComponent("main.json")
            
            //url
            let url = Bundle.main.url(forResource: "demo.json", withExtension: nil)
            
            //data
            let data = NSData(contentsOf: url!)

            //删除文件用filemanager
//            let fileManager = FileManager()
//            try! fileManager.removeItem(atPath: jsonPath)
            
            //写入磁盘
            data?.write(toFile: jsonPath, atomically: true)
            
            print("应用加载完毕 \(jsonPath)")
            
        }
    }
}

